<%-- 
    Document   : edit_product
    Created on : 14.04.2013, 15:39:57
    Author     : Yasya, Kate
--%>

<%@page import="facade.CategoryFacadeLocal"%>
<%@page import="facade.ProductFacadeLocal"%>
<%@page import="entity.Category"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="entity.Product"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%!    private ProductFacadeLocal  productFacade;
    private CategoryFacadeLocal categoryFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            productFacade = (ProductFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/ProductFacade!facade.ProductFacadeLocal");
            categoryFacade = (CategoryFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/CategoryFacade!facade.CategoryFacadeLocal");


        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>


<!DOCTYPE html>
<html>
    <head>
        <script src="scripts/productValidator.js"></script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styl_product.css"> 
        <link rel="stylesheet" type="text/css" href="css/all.css">
        
        <title>Show product</title>
    </head>
    <body>      
        <header>
          <div id ="top_name"> <a href="main.jsp"> <img src="/Delta-war/product/1.jpeg"></a> <h1> Product</h1> </div>
        </header>
        <div id="main">
            <div id="label">
                <p>Name:<font color="red">*</font></p>
                <p>Category:</p>
                <p>Price:<font color="red">*</font></p>
                 <p>Status:</p> <p>Description:</p>
                   <br>
                   <br>
                   <br>
                    <br>
                <p>Picture:</p>
            </div>
            <div id="add_product2" >  
                

<%String backURL=request.getParameter("backURL");
%>
<form id="product_form" method="post" action="EditProduct"  enctype="multipart/form-data"> 


                    <% int id_product = Integer.parseInt(request.getParameter("id_product"));
                        Product p =  productFacade.find(id_product);%>

                    <input type="hidden" name="backURL" value="<%=backURL%>"/>
                    <input type="hidden" name="id_product" value="<%= p.getId_product()%>"/>
                    <p> <input 
                            id="name_input"
                            type="text" 
                            name="name" 
                            size="50" 
                            oninput="validateName()"
                            value="<%= p.getName()%>"/>
                        <span 
                            id="name_error" 
                            style="color: red; display:none"> 
                            Name should not be empty
                        </span>
                        <span 
                            id="name_length_error" 
                            style="color: red; display:none"> 
                            Name is too long: should be less then 100 characters
                        </span>

                    </p>


                    <p> <select  name="category" size = 1> 

                            <%for (Category c : categoryFacade.findAll()) {%>
                            <option value=<%=c.getId_category()%> <%=(c.getId_category() == p.getCategory().getId_category()) ? ("selected") : ("")%> > <%= c.getName()%></option>
                            <%}%>


                        </select> </p>
                    <p> <input 
                            id="price_input"
                            type="text" 
                            name="price" 
                            size="50" 
                            oninput="validatePrice()"
                            value="<%= p.getPrice()%>" />
                        <span 
                            id="price_error" 
                            style="color: red; display:none"> 
                            Price should not be empty
                        </span>
                        <span 
                            id="price_number_error" 
                            style="color: red; display:none"> 
                            Invalid value: should be numeric and less then 100000 
                        </span>


                    </p>
                    
                     <p> <select  name="status" size = 1> 

              
                            <option value="0" <%=p.getStatus()==0 ? "selected" :""  %>>active </option>
                            <option value="1" <%=p.getStatus()==1 ? "selected" :""  %>>discontinued </option>
                           

                        </select> </p>
                    

                    
                  
                    <p> <textarea 
                            id="description" 
                            name="description" 
                            cols="39" rows="7" 
                            oninput="validateDescription()">
                            <%= p.getDescription()%>
                        </textarea>
                    
                     <span 
                            id="description_error" 
                            style="color: red; display:none"> 
                            Description is too long: should be less than 2000 
                        </span>
                        </p>
                    <p> 
                     <table>
                        <tr>
                            <td><img src=<%=p.getPicture()%>   alt= <%=p.getName()%> ></td>
                            </tr><tr>
                            <td><input type="file" name="file" /></td>
                            <td><div id="line_button">
                                  <div id="line_button"> 
                
                </div>
                               
                                </div>
                            </td>
                         
                     </table>
                </p>
                    <div id="buttons">



                    </div>


                </form>
                <div id="line_button"> 
                    <button 
                        id="func" style=" position: relative; background: #b8b0fd; left: -5px; "
                        onclick="submitForm()">
                        Ok
                    </button> 
                </div>   
                <div id="line_button">
                    <button 
                        id="func" style=" position: relative; background: #b8b0fd; left: 1%; "
                        onclick="window.location.href='catalog_product.jsp' ">
                        Cancel
                    </button> 
                </div>
            </div>

        </div>
                            <div id="footer">
                                <p>  &#169;All rights reserved</p>
                            </div>
    </body>
</html>
